package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.ReviewsComments;

public class GenericReviewsCommentDAO extends AutoKeys implements
		org.jetnuke.dao.ReviewsCommentDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericReviewsCommentDAO.class);

	public int addNew(ReviewsComments rc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(ReviewsComments) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("ReviewCommentModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("ReviewCommentModel.lastGeneratedReviewCommentID"));
		initNewReviewComment(rc, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(ReviewsComments) - end"); //$NON-NLS-1$
		}
		return id;
	}

	protected void initNewReviewComment(ReviewsComments rc, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewReviewComment(ReviewsComments, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, rc.getCOMMENTS());
		p.setTimestamp(2, new Timestamp(new Date().getTime()));
		p.setInt(3, rc.getRID());
		p.setInt(4, rc.getSCORE());
		p.setInt(5, rc.getUSERID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewReviewComment(ReviewsComments, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public ReviewsComments selectByPk(int cid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPk(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewCommentModel.selectByPK"));
		p.setInt(1, cid);
		ResultSet rs = p.executeQuery();

		rs.next();
		ReviewsComments rc = new ReviewsComments();
		fillReviewsCommentsFromResultSet(rc, rs);

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPk(int) - end"); //$NON-NLS-1$
		}
		return rc;
	}

	public List selectByRID(int rid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByRID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewCommentModel.selectByRID"));
		p.setInt(1, rid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByRID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			ReviewsComments u = new ReviewsComments();
			fillReviewsCommentsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected void fillReviewsCommentsFromResultSet(ReviewsComments rc,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillReviewsCommentsFromResultSet(ReviewsComments, ResultSet) - start"); //$NON-NLS-1$
		}

		rc.setCID(rs.getInt("CID"));
		rc.setCOMMENTS(rs.getString("COMMENTS"));
		rc.setDATE(rs.getTimestamp("DATE"));
		rc.setRID(rs.getInt("RID"));
		rc.setSCORE(rs.getInt("SCORE"));
		rc.setUSERID(rs.getInt("USERID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillReviewsCommentsFromResultSet(ReviewsComments, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void deleteByRID(int rid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByRID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewCommentModel.deleteByRID"));
		p.setInt(1, rid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByRID(int) - end"); //$NON-NLS-1$
		}
	}

	public void deleteByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewCommentModel.deleteByPK"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

}
